import {
  request,
  checkPhone,
  checkEmptyStr,
  WXshowToast,
  WXNavigateBack,
  WXRedirectTo
} from '../../utils/index.js';
import { Api } from '../../config/api.js';

Page({
  data: {
    formData: {
      phone: '',
      password: ''
    }
  },
  onLoad: function (options) {
      wx.removeStorageSync('token')
      wx.removeStorageSync('userInfo')
      wx.removeStorageSync('openid')
        wx.login({
          success: async (res) => {
            
            // wx.getUserProfile({
            //   desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            //   success: (res) => {
            //     console.log('res',res)
            //     this.setData({
            //       userInfo: res.userInfo,
            //       hasUserInfo: true
            //     })
            //   }
            // })
            
              const code = res.code
              const {data} = await request({
                  url: Api.getOpenid,
                  data: {
                      code
                  },
                  method: "POST"
              })
              if(data.data.rigister === 0){
                if(!wx.getStorageSync('login')){
                  WXRedirectTo('/pages/reg/reg')
                }
                
              }
              wx.setStorageSync("openid", data.data.openid)
          },
          fail: (err) => {

          }
      })

   },
  handleInput(e) {//公共     输入框 公共方法 把输入的值写入data
    let value = e.currentTarget.dataset.type == 'iphone'?this.validateNumber(e.detail.value):e.detail.value
    const { formData } = this.data;
    this.setData({
      formData: {
        ...formData,
         [e.currentTarget.dataset.name]: value
      }
    });
  },
  validateNumber(val) {
    return val.replace(/\D/g, '')
  },
  
async  login() {
    const { formData } = this.data;
    const isValidPhone = checkPhone(formData.phone);

    if (!isValidPhone) {
      WXshowToast('手机号或密码错误');
      return;
    }else if(!formData.password){  
          WXshowToast('请输入密码');
          return;
    }

  const {data} = await request({
      url: Api.login,
      data: formData,
      method: 'POST'
    })
    if(data.code==1001){
          WXshowToast("对不起您输入的密码不正确");
    }else{
                //  wx.login({
                //      success: async (res) => {
                //          const code = res.code
                //          const {data} = await request({
                //              url: Api.getOpenid,
                //              data: {
                //                  code
                //              },
                //              method: "POST"
                //          })
                //          wx.setStorageSync("openid", data.data.openid)
                //      },
                //      fail: (err) => {

                //      }
                //  })
            
              wx.setStorageSync('token', data.data.jwt_token);
              wx.setStorageSync('userInfo', data.data);
              wx.redirectTo({
                  url: '/pages/index/index'
              });
    }
   
  },
  bindGetUserInfo: function (e) {
    console.log('login')
    if (e.detail.userInfo) {
        //用户按了允许授权按钮
        var that = this;
        wx.login({
          success: async (res) => { 
             console.log(res)
              const { data } = await request({
                  url: Api.getOpenid,
                  method: 'POST',
                  data: {
                      code:res.code
                  }
              })
              if(data){
                wx.setStorageSync('token', data.data.jwt_token);
                wx.setStorageSync('userInfo', data.data);
                wx.redirectTo({
                    url: '/pages/index/index'
                });
              }
          }
      })
    } else {
        //用户按了拒绝按钮
        wx.showModal({
            title:'警告',
            content:'您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
            showCancel:false,
            confirmText:'返回授权',
            success:function(res){
                if (res.confirm) {
                    console.log('用户点击了“返回授权”')
                } 
            }
        })
    }
},


});
